package com.hehe.controller.QQ_space;

import com.hehe.Common.Upload_images;
import com.hehe.POJO.Qq_space;
import com.hehe.POJO.User;
import com.hehe.Repository.QQspaceRepository;
import com.hehe.Repository.UserRepository;
import com.hehe.Service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Slf4j
@Controller
@RequestMapping(value = "")
public class QQ_Space_Add_Controller {


    @Value("${file.upload.path}")
    private String SAVE_FOLDER_PATH;


    @Value("${config_file_name}")
    private String config_file_name;

    @Resource
    private QQspaceRepository qQspaceRepository;

    @Resource
    private UserRepository userRepository;

    @Resource
    private UserService userService;


    @GetMapping(value = "/qq-space-add")
    public String index(HttpServletRequest request, Model model) {
        log.info("qq-space-add");
        String username = userService.get_username_by_cookie(request);
        if (username != null) {
            User user = userRepository.findByUsername(username);
            if (user.getRole().equals("admin")) {
                return "qq-space/qq-space-add.html";
            }
        }
        return "index.html";
    }


    @PostMapping("/qq-space-add/upload")
    public ResponseEntity<?> handleFileUpload(
            HttpServletRequest request,
            @RequestParam("text") String text,
            @RequestParam("files") MultipartFile[] files) {

        System.out.println("Received text: " + text);

        if (files.length == 0) {
            return ResponseEntity.ok("No files uploaded.");
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String timestamp = sdf.format(new Date());
        System.out.println(timestamp);               //  20240714073648

        String father_foler = "QQ-space/" + timestamp + "/";  // 文件夹

        ArrayList<String> filePathList = Upload_images.saveImagesWithWatermarkandCompress(SAVE_FOLDER_PATH + father_foler, files, 100, "AAAbbb233.com");

        if (filePathList == null) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error occurred while saving files.");
        }

        StringBuilder filePaths_str = new StringBuilder();
        // 遍历 filePathList
        // ResourceConfig 配置映射路径 images
        for (String filePath : filePathList) {
//           访问全路径  http://192.168.8.194:8055/images/b4852718-f57e-474f-af37-fe5aad03d973.jpg
//            String file_URL = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/images/" + filePath;
//            System.out.println("file_URL = " + file_URL);
            String full_path = father_foler + filePath;
            System.out.println("full_path = " + full_path);
            filePaths_str.insert(0, full_path + ",");
        }
        Qq_space qqSpace = new Qq_space();
        qqSpace.setDescription(text);
        qqSpace.setImg_path(filePaths_str.toString());
        qQspaceRepository.save(qqSpace);
        return ResponseEntity.ok(filePathList);
    }















//    dfsdfsdf

}